package com.itheima.studenthomework.controller;

import com.itheima.studenthomework.domain.Student;
import com.itheima.studenthomework.service.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherController {
    public void start() {
        Scanner sc = new Scanner(System.in);

        loop:
        while (true) {
            System.out.println("=========欢迎来到老师管理系统=========");
            System.out.println("1.添加学生");
            System.out.println("2.修改学生");
            System.out.println("3.删除学生");
            System.out.println("4.查看学生");
            System.out.println("5.返回上一级");
            System.out.println("6.退出");
            String str = sc.next();

            switch (str){
                case "1":
                    addStudent();
                    break;
                case "2":
                    updateStudent();
                    break;
                case "3":
                    deleteStudent();
                    break;
                case "4":
                    findStudent();
                    break;
                case "5":
                    System.out.println("返回上一级");
                    break loop;
                case "6":
                    System.exit(0);
                default:
                    System.out.println("您的输入有误，请重新输入！");
            }
        }
    }

    private void deleteStudent() {
        Scanner sc = new Scanner(System.in);

        StudentService studentService = new StudentService();
        System.out.println("请输入学生id：");
        String id = sc.next();
        int index = studentService.isExist(id);

        if(index == -1){
            System.out.println("学生信息不存在");
            return;
        }

        boolean flag = studentService.deleteStudent(index);

        if(flag){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }

    private void updateStudent() {
        Scanner sc = new Scanner(System.in);

        StudentService studentService = new StudentService();
        System.out.println("请输入学生id：");
        String id = sc.next();
        int index = studentService.isExist(id);

        if(index == -1){
            System.out.println("学生信息不存在");
            return;
        }

        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        String age = sc.next();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();

        Student stu = new Student(id,name,age,birthday);
        boolean flag = studentService.updateStudent(stu);

        if(flag){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
    }

    private void findStudent() {
        StudentService studentService = new StudentService();

        ArrayList<Student> list = studentService.findStudent();
        if(list == null){
            System.out.println("当前没有学生数据，请添加后再查看！");
            return;
        }

        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student.getId()
                    +"\t"+student.getName()
                    +"\t"+student.getAge()
                    +"\t"+student.getBirthday());
        }



    }

    private  void addStudent() {
        Scanner sc = new Scanner(System.in);

        StudentService studentService = new StudentService();
        System.out.println("请输入学生学号：");
        String id;
        while (true){
            id = sc.next();
            int index = studentService.isExist(id);
            if(index == -1){
                break;
            }else {
                System.out.println("学号已存在，请重新输入");
            }
        }
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        String age = sc.next();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();

        Student stu = new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);

        boolean flag = studentService.addStudent(stu);

        if(flag){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }


}
